package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {
  
  @Autowired
  CategoryMapper categoryMapper;
  
  /**
   * 分页查询
   * @param queryDTO
   */
  @Override
  public PageResult page(CategoryPageQueryDTO queryDTO) {
    PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
    Page<Category> page = categoryMapper.page(queryDTO);
    return new PageResult(page.getTotal(), page.getResult());
  }
  
  /**
   * 新增分类
   * @param categoryDTO
   */
  @Override
  public void add(CategoryDTO categoryDTO) {
    Category category = new Category();
    BeanUtils.copyProperties(categoryDTO, category);
    category.setStatus(1);
    categoryMapper.insert(category);
  }
  
  /**
   * 修改
   */
  @Override
  public void update(CategoryDTO categoryDTO) {
    Category category = new Category();
    BeanUtils.copyProperties(categoryDTO, category);
    categoryMapper.update(category);
  }
  
  /**
   * 修改状态
   */
  @Override
  public void changeStatus(Integer status, Long id) {
    Category category = Category.builder().id(id).status(status).build();
    categoryMapper.update(category);
  }
  
  /**
   * 根据id删除
   * @param id
   */
  @Override
  public void del(Long id) {
    categoryMapper.del(id);
  }
  
  /**
   * 根据类型查询分类
   * @param type 1 菜品分类 2套餐分类
   */
  @Override
  public List<Category> findByType(String type) {
    return categoryMapper.findByType(type);
  }
}
